package com.powernode.controller;

import com.powernode.dto.Result;
import com.powernode.entity.Category;
import com.powernode.entity.Product;
import com.powernode.service.CategoryService;
import com.powernode.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import java.util.List;

@RestController
@RequestMapping("/pms/app")
@Validated
public class AppPmsController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据商品id获取商品集合
     */
    @GetMapping("/products/listByIds")
    public Result<List<Product>> listProductsByIds(@RequestParam List<Long> productIds) {

        return Result.success(productService.listProductsByIds(productIds));
    }

    /**
     * 根据商品id获取单个商品
     */
    @GetMapping("/products/{productId}")
    public Result<Product> getProductById(@PathVariable Long productId) {
        return Result.success(productService.getProductById(productId));
    }

    /**
     * 根据商品分类层级获取商品分类集合
     */
    @GetMapping("/categories/listByLevel")
    public Result<List<Category>> listCategoriesByLevel(@RequestParam Integer level) {
        return Result.success(categoryService.listCategoriesByLevel(level));
    }

    /**
     * 增加库存
     * @Min(1) 校验必须大于1
     */
    @PutMapping("/products/stock/incr/{productId}/{num}")
    public Result incrStock(@PathVariable Long productId,@PathVariable @Min(1) Integer num) {
        return Result.success("增加库存成功",productService.incrStock(productId,num));
    }

    /**
     * 减少库存
     */
    @PutMapping("/products/stock/decr/{productId}/{num}")
    public Result decrStock(@PathVariable Long productId, @PathVariable @Min(1) Integer num){
        return Result.success("减少库存成功",productService.decrStock(productId,num));
    }


}
